class Game
{
    public test:Test;
    public renderer;
    public stage:PIXI.Container;
    constructor()
    {

    }
    /**
     * start
     */
    public start() {
        console.log("start aaa ");

        this.renderer = PIXI.autoDetectRenderer(800, 600);
        document.body.appendChild(this.renderer.view);
        this.stage = new PIXI.Container();
        this.renderer.render(this.stage);
        PIXI.loader.add("head","images/head.png");
        PIXI.loader.add("ppdata","images/data.json");
        PIXI.loader.add("texJson","images/Dragon_tex.json");
        PIXI.loader.add("texPng","images/Dragon_tex.png");
        PIXI.loader.once("complete", this._loadComplateHandler, this);
        PIXI.loader.load();
        // function setup() {
        //     var sprite = new PIXI.Sprite(PIXI.loader.resources["images/head.png"].texture);
        //     //stage.addChild(sprite);
            
        // }

    }
    private _loadComplateHandler(): void {
        var sprite = new PIXI.Sprite(PIXI.loader.resources["head"].texture);
        // this.stage.addChild(sprite);
       
        var ppd = PIXI.loader.resources["ppdata"].data;

        var frame:PIXI.Rectangle = new PIXI.Rectangle(0,0,48,48);
        var ori:PIXI.Rectangle = new PIXI.Rectangle(0,0,50,50);
        var trim:PIXI.Rectangle = new PIXI.Rectangle(0,0,50,50);

        var t:PIXI.Texture = new PIXI.Texture(sprite.texture.baseTexture,frame,null,null)
        var s = new PIXI.Sprite(t);
       //this.stage.addChild(s);
         //this.reDraw();
        // console.log(ppd);
         //this.initBox2d();
         this.initLiquidfun();
         //this.playDemo();
    }
    public playDemo():void
    {

        var debug = new DebugDraw();

        this.stage.addChild(debug.graphics);
        //this.test = new TestBridge(debug);
        // this.test = new TestBreakable(debug);
        // this.test = new TestBuoyancy(debug);
        // this.test = new TestCCD(debug);
        // this.test = new TestCompound(debug);
         this.test = new TestCrankGearsPulley(debug);
        // this.test = new TestEdges(debug);
        // this.test = new TestOneSidePlatform(debug);
        // this.test = new TestRagdoll(debug);
        // this.test = new TestRaycast(debug);
        // this.test = new TestStack(debug);
        //this.test = new TestTheoJansen(debug);

        PIXI.ticker.shared.add(this.gameLoop, this);
        this.stage.interactive = true;

    }
    public initBox2d():void
    {
        var f = new pengpeng.PixiFactory();
        var ppd = PIXI.loader.resources["ppdata"].data;
        f.addPengPengData(ppd);
        f.addTextueData(PIXI.loader.resources["texJson"].data, 
                        PIXI.loader.resources["texPng"].texture)
        var w:pengpeng.PixiBox2DWorld = f.createWorld("world1") as pengpeng.PixiBox2DWorld;

        this.stage.addChild(w.worldView)
        let b = f.getTextureDisplay("parts/head");
        b.x = 100;
        // this.stage.addChild(b);
        console.log(b, b.width,b.height);
        // this.reDraw();
        w.run();
        PIXI.ticker.shared.add(this.gameLoop, this);
    }

    public initLiquidfun():void
    {
        console.log("liquidfun start");
        var liquidfunTest:TestLiquidfun = new TestLiquidfun();
        liquidfunTest.initTestbed();
    }
    public reDraw():void
    {
        if(this.renderer && this.stage)
        {
            this.renderer.render(this.stage);
        }
    }
    public gameLoop():void
    {
        //var that = this;
        //requestAnimationFrame(function():void{that.gameLoop()});
        if(this.test)
        {
            this.test.update();
        }
        this.reDraw();
    }
}